package com.voto.sunflower.activity.me;

import java.util.Calendar;

import retrofit.RetrofitError;
import retrofit.client.Response;
import android.app.DatePickerDialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.voto.sunflower.R;
import com.voto.sunflower.SunflowerApplication;
import com.voto.sunflower.activity.BaseActivity;
import com.voto.sunflower.model.request.UpdateMe;
import com.voto.sunflower.model.response.UserResponse;
import com.voto.sunflower.retrofit.ClientHttpService;
import com.voto.sunflower.retrofit.NetworkHandler;
import com.voto.sunflower.retrofit.RestError;

public class ModifyBirthDayActivity extends BaseActivity implements OnClickListener{

	public final static String TAG= "ModifyBirthdayActivity";
	private TextView birthdayTextView;
	private int mBirthdayArr[] = new int[3];
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_birthday);
		initView();
		setUserInfo();
	}
	
	private void setUserInfo() {
		String birthday = getIntent().getExtras().getString("birthday");
		String birthdayArrTemp[] = birthday.split("-");
		if(birthdayArrTemp.length == 3){
			for(int i=0;i<3;i++){
				mBirthdayArr[i] = Integer.valueOf(birthdayArrTemp[i]);
			}
		}else{
			mBirthdayArr[0] = 1980;
			mBirthdayArr[1] = 1;
			mBirthdayArr[2] = 1;
		}
		birthdayTextView.setText(birthday);
	}

	private void initView() {
		TextView back = (TextView) findViewById(R.id.back);
		TextView title = (TextView) findViewById(R.id.title);
		TextView other = (TextView) findViewById(R.id.other);
		
		birthdayTextView =(TextView) findViewById(R.id.birthday_text);
		
		
		String birthday = getIntent().getExtras().getString("birthday");
		birthdayTextView.setHint(birthday);
		
		findViewById(R.id.btn).setOnClickListener(this);
		Resources rs = getResources();
		back.setOnClickListener(this);
		title.setText(rs.getString(R.string.edit_birthday));
		other.setText(rs.getString(R.string.save));
		other.setVisibility(View.VISIBLE);
		other.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
			switch (v.getId()) {
			case R.id.back:
				finish();
				break;
			case R.id.other:
				updateUserInfo();
				break;
			case R.id.btn:
				chooseDate();
				break;
			default:
				break;
			}
	}

	/**
	 * 失去焦点
	 */
	public void loseFocus(View view) {	
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
	}
	
	protected void updateUserInfo() {
		UpdateMe update = new UpdateMe();
		String birthday = birthdayTextView.getText().toString();
		update.setBirthday(birthday);
		ClientHttpService.getMeService().updateMe(update, new UpdateCallback());
	}
	
	/**
	 * 选择日期
	 * 
	 * @param view
	 */
	public void chooseDate() {
		Calendar calendar = Calendar.getInstance();
		DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker datePicker, int year, int month,
					int dayOfMonth) {
				String mBirthday = year + "-" + (month + 1) + "-" + dayOfMonth;
				birthdayTextView.setText(mBirthday);
			}
		};
		DatePickerDialog dialog = new DatePickerDialog(
				ModifyBirthDayActivity.this, dateListener,
				mBirthdayArr[0], mBirthdayArr[1],
				mBirthdayArr[2]);
		dialog.show();
	}
	
	class UpdateCallback extends NetworkHandler<UserResponse> {

	@Override
			public void failure(RetrofitError error) {
				Log.d(TAG , "update birthday fail------>");
				if (error.getResponse() != null) {
					int statusCode = error.getResponse().getStatus();
					if (statusCode == 500 || statusCode == 501) {
						final RestError body = (RestError) error.getBodyAs(RestError.class);
						Toast.makeText(ModifyBirthDayActivity.this, body.errorDetails, Toast.LENGTH_SHORT).show();
					} else if (statusCode == 408) {
						Toast.makeText(ModifyBirthDayActivity.this, "连接超时", Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(ModifyBirthDayActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
			}

		@Override
		public void success(UserResponse userResponse, Response arg1) {
			super.success(userResponse, arg1);
			Log.d(TAG , "update birthday sucess------>");
			Toast.makeText(getApplicationContext(), "成功修改", Toast.LENGTH_SHORT)
					.show();
			SunflowerApplication.setmUser(userResponse.getUser());
			finish();
		}

	}

}
